package com.ms.p2p.controller.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.exception.NoticeException;
import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.Constants;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.system.AdminLog;
import com.ms.p2p.entity.system.AdminUser;
import com.ms.p2p.provider.AdminLogProvider;
import com.ms.p2p.provider.AdminUserProvider;
import com.ms.p2p.provider.RbacProvider;
import com.ms.p2p.utils.HttpTookit;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;

/**
 * Created by xing_ on 2017/6/7.
 */
@Controller("adminLoginController")
@RequestMapping("/a")
public class AdminLoginController extends AdminBaseController {

    @Reference
    private AdminUserProvider es;
    @Reference
    private RbacProvider rbacProvider;
    @Reference
    AdminLogProvider loginLogService;

    @RequestMapping("/login")
    public String login() {
        return "admin/login";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest req) {
        HttpSession ht=req.getSession(true);
        ht.removeAttribute(Constants.PRINCIPAL_ADMIN_NAME);
        return "admin/login";
    }

    @RequestMapping("/loginVerify")
    @ResponseBody
    public Message login(String username, String password, String code,
                         HttpServletRequest request, HttpSession session) {

        String serviceCode = (String) session.getAttribute(Constants.CAPTCHA_SESSION_KEY);

        AdminLog loginLog = new AdminLog();
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        loginLog.setDevice(userAgent.toString());
        loginLog.setIp(AppContext.getRemoteIp());
        loginLog.setOpera_username(username);
        loginLog.setOpera("登陆");
        loginLog.setRemark("登陆");
        loginLog.setTime(MsDateUtils.getTime());

        if (StringUtils.isBlank(code)) {
            throw  new NoticeException("验证码错误");
        }
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            throw  new NoticeException("用户名密码不能为空");
        } else if (StringUtils.isBlank(serviceCode) || !StringUtils.equalsIgnoreCase(serviceCode, code)) {
            throw  new NoticeException("验证码错误");
        } else {
            AdminUser user = es.selectByadmin_name(username);
            if (user == null) {
                throw  new NoticeException("用户名密码错误");
            } else if (!password.equals(user.getPassword())) {
                loginLog.setStatus(0);
                loginLogService.insert(loginLog);
                throw  new NoticeException("用户名密码错误");
            } else {
                loginLog.setStatus(1);
                loginLogService.insert(loginLog);
                session.setAttribute(Constants.PRINCIPAL_ADMIN_NAME, user);
                return Message.success("登录成功");
            }
        }
    }

    @Override
    public boolean getBinderFlag() {
        return true;
    }
}
